package com.bw.paper.mvp.view.Fragment;

import android.Manifest;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.ImageView;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.paper.mvp.view.Activity.ChannelActivity;
import com.bw.paper.mvp.view.Fragment.news.Adapter.NewsVpAdapter;
import com.bw.paper.mvp.view.Fragment.news.Adapter.TextBannerAdapter;
import com.bw.paper.App;
import com.bw.paper.mvp.view.Fragment.news.ContentNewsFragment;
import com.bw.paper.R;
import com.bw.paper.entity.ChannelEntity;
import com.bw.paper.entity.TextBannerEntity;
import com.bw.paper.entity.UserEntity;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.view.BaseFragment;
import com.bw.paper.mvp.contact.LREContract;
import com.bw.paper.mvp.di.DaggerLREComponent;
import com.bw.paper.mvp.di.LREModules;
import com.bw.paper.mvp.model.HttpCode;
import com.bw.paper.mvp.presenter.LREPresenter;
import com.flyco.tablayout.SlidingTabLayout;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作业：新闻首页吸顶效果协调者布局完成;
 * 新闻首页列表完成;
 * Glide图片异步加载图片工具类封装;
 * LRE逻辑编写
 * git代码仓库提交代码
 */
public class NewsFragment extends BaseFragment<LREPresenter>
        implements LREContract.ILREView,
        OnBannerListener<TextBannerEntity.Values>, View.OnClickListener {
    private SlidingTabLayout tabLayout;
    private ViewPager vp;
    private Banner<TextBannerEntity.Values, TextBannerAdapter> news_text_banner;
    private TextBannerAdapter textBannerAdapter;
    private NewsVpAdapter newsVpAdapter;
    private List<Fragment> fragments;
    private ImageView newsIcon;
    private ImageView newsAudioImg;
    private ImageView newsSearchIcon;
    private Banner newsTextBanner;
    private ImageView newsMoreImg;


    @Override
    public int bindlayout() {
        return R.layout.fragment_news;
    }

    @Override
    public void initView() {
//        tabLayout = f(R.id.news_tab);
//        vp = f(R.id.news_vp);
//        news_text_banner = f(R.id.news_text_banner);
        newsIcon = (ImageView) f(R.id.news_icon);
        newsAudioImg = (ImageView) f(R.id.news_audio_img);
        newsSearchIcon = (ImageView) f(R.id.news_search_icon);
        newsMoreImg = (ImageView) f(R.id.news_more_img);

        tabLayout = (SlidingTabLayout) f(R.id.news_tab);
        vp = (ViewPager) f(R.id.news_vp);
        news_text_banner = (Banner) f(R.id.news_text_banner);

        newsMoreImg.setOnClickListener(this::onClick);

    }

    @Override
    public void initData() {
        fragments = new ArrayList<>();
        //获取顶部文字banner数据源
        List<UserEntity> users = App.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
        boolean flag = users.size() == 0;
        //获取频道数据源
        Map<String, Object> bannerMap = new HashMap<>();
        bannerMap.put("code", HttpCode.TEXTBANNERCODE);
        Map<String, Object> channelMap = new HashMap<>();
        channelMap.put("code", HttpCode.USERCHANNELCODE);
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        channelMap.put("imie", flag ? PhoneUtils.getIMEI() : "");
        channelMap.put("userId",1);
        p.requestAll(bannerMap, channelMap);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this)).build()
                .injectNewsFragment(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        news_text_banner.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        news_text_banner.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        news_text_banner.destroy();
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof TextBannerEntity) {
            //文字banner返回
            TextBannerEntity textBannerEntity = (TextBannerEntity) entity;
            LogUtils.e("textBannerEntity:" + textBannerEntity.values);
            if (textBannerEntity.getValues() == null) {
                ToastUtils.showShort("文字广告数据丢失");
                return;
            }
            textBannerAdapter = new TextBannerAdapter(textBannerEntity.getValues());
            news_text_banner.setAdapter(textBannerAdapter)
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(new ZoomOutPageTransformer())
                    .setOnBannerListener(this);
        } else if (entity instanceof ChannelEntity) {
               //频道列表数据
            //按照频道数量以及频道id初始化fragments
            ChannelEntity channelEntity = (ChannelEntity) entity;
            if (channelEntity.getValues() == null) {
                ToastUtils.showShort("无频道数据");
                return;
            }
            for (int i = 0; i < channelEntity.getValues().size(); i++) {
                fragments.add(new ContentNewsFragment(channelEntity.getValues().get(i).channel_id));
            }
            newsVpAdapter = new NewsVpAdapter(getFragmentManager(), fragments, channelEntity.getValues());
            vp.setAdapter(newsVpAdapter);
            tabLayout.setViewPager(vp);
        }
    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {

    }

    @Override
    public void OnBannerClick(TextBannerEntity.Values data, int position) {
        ToastUtils.showShort("点击广告:" + data.news_title);
    }

    @Override
    public void onClick(View v) {
        startPage(ChannelActivity.class);
    }
}